package com.comm.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 公共模块
 * @author dengxuzhao
 * @time 2016-04-24
 */
public class PubMethodsUtil {
	
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 获取系统当前时间
	 * @return 当前时间字符串
	 */
	public static String getCurrTime(){
		return dateFormat.format(new Date());
	}
	
	/**
	 * 将汉字转换为全拼
	 * @param toConvertStr 待转换字符串
	 */
	public static String getPingYin(String toConvertStr) {
		char[] toConChar = null;
		toConChar = toConvertStr.toCharArray();
		int t0 = toConChar.length;
		
		String[] toConCtr= new String[toConChar.length];
		
		HanyuPinyinOutputFormat toConCtrFormat = new HanyuPinyinOutputFormat();
		toConCtrFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		toConCtrFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		toConCtrFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
		
		String ComStr = "";
		try {
			for (int i = 0; i < t0; i++) {
				// 判断是否为汉字字符
				if (java.lang.Character.toString(toConChar[i]).matches("[\\u4E00-\\u9FA5]+")) {
					toConCtr = PinyinHelper.toHanyuPinyinStringArray(toConChar[i], toConCtrFormat);
					ComStr += toConCtr[0];
				} else
					ComStr += java.lang.Character.toString(toConChar[i]);
			}
			return ComStr;
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}
		return ComStr;
	}

	/**
	 * 返回中文的首字母
	 * @Param conStr 待转字符串
	 */
	public static String getPinYinHeadChar(String conStr) {
		String convert = "";
		if(conStr != null){
			for (int j = 0;j < conStr.length();j++) {
				char word = conStr.charAt(j);
				String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
				if (pinyinArray != null) {
					convert += pinyinArray[0].charAt(0);
				} else {
					convert += word;
				}
			}
		}
		return convert.toUpperCase();
	}

}
